<script setup>
import { ref } from 'vue'
//数据回显 个人的信息存入了pinia中 把数据从pinia中拿出来
import useUserInfoStore from '@/stores/userinfo.js'
const userInfoStore = useUserInfoStore()
//userInfoStore的info属性就是个人信息

//数据模型 包裹了一个js对象 将个人信息解构即可
const userInfo = ref({
    ...userInfoStore.info,
    // id: 0,
    // username: 'zhangsan',
    // nickname: 'zs',
    // email: 'zs@163.com',
})
const rules = {
    name: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ]
}
import { ElMessage } from 'element-plus'
import {userInfoUpdateService} from '@/api/user.js'
//修改个人信息
const updateUserInfo = async()=>{
    //调用接口
    let result = await userInfoUpdateService(userInfo.value)
    ElMessage.success(result.msg? result.msg:'修改成功')

    //修改pinia中的个人信息
    userInfoStore.setInfo(userInfo.value)
}


</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <!--登录身份证号不可修改 其他可改 v-model就已经完成了数据的回写 因为已经解构-->
                    <el-form-item label="身份证号">
                        <el-input v-model="userInfo.idCard" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="userInfo.name" disabled></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="是否为学生">
                    <el-select v-model="userInfo.student" placeholder="请选择是否为学生">
                        <el-option label="是" value="true"></el-option>
                        <el-option label="否" value="false"></el-option>
                    </el-select>
                    </el-form-item> -->
                    <el-form-item label="年龄">
                        <el-input v-model="userInfo.age"></el-input>
                    </el-form-item>
                    <el-form-item label="电话">
                        <el-input v-model="userInfo.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input v-model="userInfo.address"></el-input>
                    </el-form-item>
                    <el-form-item label="性别">
                    <el-select v-model="userInfo.sex" placeholder="请选择性别">
                        <el-option label="男" value="男"></el-option>
                        <el-option label="女" value="女"></el-option>
                    </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo()">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>